#include "str_operation.hpp"
#include <cctype>

using namespace std;

int total_appear_times(const string &str1, const string &str2)
{
    int endpos = str1.size() - str2.size();
    if (endpos < 0)
        return 0;
    int t = 0;

    for (int i = 0; i <= endpos; i++)
    {
        int j = 0;
        for ( ; j != str2.size(); j++)
        {
            if (str1[i + j] != str2[j])
            {
                i += j;
                break;
            }
        }
        if (str2.size()  == j) {
            i += str2.size() - 1;
            t++;
        }
    }
    return t;
}

void replace_split_with_blank(string &words, const string &split)
{
    for (int i = 0; i != words.size(); i++)
        for (int j = 0; j != split.size(); j++)
            if (words[i] == split[j])
            {
                words[i] = ' ';
                break;
            }
}

void replace_some_words(string &words)
{
    int pos;
    for (int i = 0; i != words.size(); i++)
        //it's not a number. I will do...
        if (ispunct(words[i]) || isalpha(words[i]))
        {
            pos = i;
            while (!isblank(words[pos]) && pos >= 0)
                words[pos--] = ' ';
            pos = i + 1;
            while (!isblank(words[pos]) && pos != words.size())
                words[pos++] = ' ';
        }
}
